﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PriceLibrary.PriceService;

namespace VDataAdapter.Communication
{
    [TestClass]
    public class TestVDataMessageBuilder
    {
        /// <summary>
        //BasicPrice
        //Ceiling
        //DateNo
        //Floor
        //FloorCode
        //ListingStatus
        //PrevPriorPrice
        //PriorClosePrice
        //PriorPrice
        //SecurityName
        //StockType
        //TotalListingQtty
        //TradingUnit
        //AccumulatedVal
        //AccumulatedVol
        //AdjustQtty
        //AdjustRate
        //AvrPrice
        //Best1Bid
        //Best1BidVolume
        //Best1Offer
        //Best1OfferVolume
        //Best2Bid
        //Best2BidVolume
        //Best2Offer
        //Best2OfferVolume
        //Best3Bid
        //Best3BidVolume
        //Best3Offer
        //Best3OfferVolume
        //BidCount
        //BuyForeignQtty
        //BuyForeignValue
        //ClosePrice
        //CurrentRoom
        //DividentRate
        //Highest
        //Last
        //LastVal
        //LastVol
        //Lowest
        //OfferCount
        //OpenPrice
        //ProjectOpen
        //PtMatchPrice
        //PtMatchQtty
        //PtTotalTradedQtty
        //PtTotalTradedValue
        //ReferenceStatus
        //SellForeignQtty
        //SellForeignValue
        //Status
        //StockSymbol
        //Time
        //TotalBidQtty
        //TotalOfferQtty
        //TotalRoom
        //TotalShare
        //TotalValue
        /// </summary>
        [TestMethod]
        public void TestBuildStockMessageShouldReturnPropertyInCorrectOrder()
        {
            StockInfoDyns stock = new StockInfoDyns();

            string expected = "0.1|0.2|3|0.4|0.5|0.6|0.7|0.8|0.9|10|1.1|1.2|1.3|1.4|1.5|1.6|1.7|1.8|1.9|20|2.1|";
            expected += "2.2|2.3|2.4|2.5|2.6|2.7|2.8|29|30|3.1|3.2|33|34|3.5|36|3.7|3.8|39|4.0|4.1|42|43|4.4|4.5|4.6|";
            
            stock.AccumulatedVal = 0.1;
            stock.AccumulatedVol = 0.2;
            stock.AdjustQtty = 3;
            stock.AdjustRate = 0.4;
            stock.AvrPrice = 0.5;
            stock.Best1Bid = 0.6; 
            stock.Best1BidVolume = 0.7;
            stock.Best1Offer = 0.8;
            stock.Best1OfferVolume = 0.9;
            stock.Best2Bid = 10;
            stock.Best2BidVolume = 1.1;
            stock.Best2Offer = 1.2;
            stock.Best2OfferVolume = 1.3;
            stock.Best3Bid = 1.4;
            stock.Best3BidVolume = 1.5;
            stock.Best3Offer = 1.6;
            stock.Best3OfferVolume = 1.7;
            stock.BidCount = 1.8;
            stock.BuyForeignQtty = 1.9;
            stock.BuyForeignValue = 20;
            stock.ClosePrice = 2.1;
            stock.CurrentRoom = 2.2;
            stock.DividentRate = 2.3;
            stock.Highest = 2.4;
            stock.Last = 2.5;
            stock.LastVal = 2.6;
            stock.LastVol = 2.7;
            stock.Lowest = 2.8;
            stock.OfferCount = 29;
            stock.OpenPrice = 30;
            stock.ProjectOpen = 3.1;
            stock.PtMatchPrice = 3.2;
            stock.PtMatchQtty = 33;
            stock.PtTotalTradedQtty = 34;
            stock.PtTotalTradedValue = 3.5;
            stock.ReferenceStatus = 36;
            stock.SellForeignQtty = 3.7;
            stock.SellForeignValue = 3.8;
            stock.Status = 39;
            stock.StockSymbol = "4.0";
            stock.Time = "4.1";
            stock.TotalBidQtty = 42;
            stock.TotalOfferQtty = 43;
            stock.TotalRoom = 4.4;
            stock.TotalShare = 4.5;
            stock.TotalValue = 4.6;

            string actual = StockMessageBuilder.BuildMessage(stock);
            Assert.AreEqual(expected, actual, "Message format is wrong");
                
        }

        [TestMethod]
        public void TestBuildStockMessageShouldReturnStringNullForValueWhichIsNull()
        {
            StockInfoDyns stock = new StockInfoDyns();

            string expected = "0.1|0.2|3|0.4|0.5|0.6|0.7|0.8|0.9|10|1.1|1.2|1.3|1.4|1.5|1.6|1.7|1.8|1.9|20|2.1|";
            expected += "2.2|2.3|2.4|2.5|2.6|2.7|2.8|29|30|3.1|3.2|33|34|3.5|36|3.7|3.8|39|Null|Null|42|43|4.4|4.5|4.6|";

            stock.AccumulatedVal = 0.1;
            stock.AccumulatedVol = 0.2;
            stock.AdjustQtty = 3;
            stock.AdjustRate = 0.4;
            stock.AvrPrice = 0.5;
            stock.Best1Bid = 0.6;
            stock.Best1BidVolume = 0.7;
            stock.Best1Offer = 0.8;
            stock.Best1OfferVolume = 0.9;
            stock.Best2Bid = 10;
            stock.Best2BidVolume = 1.1;
            stock.Best2Offer = 1.2;
            stock.Best2OfferVolume = 1.3;
            stock.Best3Bid = 1.4;
            stock.Best3BidVolume = 1.5;
            stock.Best3Offer = 1.6;
            stock.Best3OfferVolume = 1.7;
            stock.BidCount = 1.8;
            stock.BuyForeignQtty = 1.9;
            stock.BuyForeignValue = 20;
            stock.ClosePrice = 2.1;
            stock.CurrentRoom = 2.2;
            stock.DividentRate = 2.3;
            stock.Highest = 2.4;
            stock.Last = 2.5;
            stock.LastVal = 2.6;
            stock.LastVol = 2.7;
            stock.Lowest = 2.8;
            stock.OfferCount = 29;
            stock.OpenPrice = 30;
            stock.ProjectOpen = 3.1;
            stock.PtMatchPrice = 3.2;
            stock.PtMatchQtty = 33;
            stock.PtTotalTradedQtty = 34;
            stock.PtTotalTradedValue = 3.5;
            stock.ReferenceStatus = 36;
            stock.SellForeignQtty = 3.7;
            stock.SellForeignValue = 3.8;
            stock.Status = 39;
            stock.StockSymbol = null;
            stock.Time = null;
            stock.TotalBidQtty = 42;
            stock.TotalOfferQtty = 43;
            stock.TotalRoom = 4.4;
            stock.TotalShare = 4.5;
            stock.TotalValue = 4.6;

            string actual = StockMessageBuilder.BuildMessage(stock);
            Assert.AreEqual(expected, actual, "Message format is wrong");

           

        }

        [TestMethod]
        public void TestBuildMessageStaticStockObject()
        {
            StockInfoStatic stock = new StockInfoStatic();

            string expected = "0.1|0.2|3|0.4|05|6|0.7|0.8|0.9|10|11|12|13|";

            stock.BasicPrice = 0.1;
            stock.Ceiling = 0.2;
            stock.DateNo  = 3;
            stock.Floor = 0.4;
            stock.FloorCode = 5;
            stock.ListingStatus = 6;
            stock.PrevPriorPrice = 0.7;
            stock.PriorClosePrice = 0.8;
            stock.PriorPrice = 0.9;
            stock.SecurityName = "10";
            stock.StockType = "11";
            stock.TotalListingQtty = 12;
            stock.TradingUnit = 13;

            string actual = StockMessageBuilder.BuildMessage(stock);

            Assert.AreEqual(expected, actual, "Format message incorrect");
        }

        [TestMethod]
        public void TestStockInfoStaticReturnNullStringForNullProperty()
        {
            StockInfoStatic stock = new StockInfoStatic();

            string expected = "0.1|0.2|3|0.4|05|6|0.7|0.8|0.9|Null|Null|12|13|";

            stock.BasicPrice = 0.1;
            stock.Ceiling = 0.2;
            stock.DateNo = 3;
            stock.Floor = 0.4;
            stock.FloorCode = 5;
            stock.ListingStatus = 6;
            stock.PrevPriorPrice = 0.7;
            stock.PriorClosePrice = 0.8;
            stock.PriorPrice = 0.9;
            stock.SecurityName = null;
            stock.StockType = null;
            stock.TotalListingQtty = 12;
            stock.TradingUnit = 13;

            string actual = StockMessageBuilder.BuildMessage(stock);

            Assert.AreEqual(expected, actual, "Format message incorrect");
        }

        private static StockInfoStatic GetSpeicalStockInfoStatic()
        {
            StockInfoStatic stock = new StockInfoStatic();

            stock.BasicPrice = 0.1;
            stock.Ceiling = 0.2;
            stock.DateNo = 3;
            stock.Floor = 0.4;
            stock.FloorCode = 5;
            stock.ListingStatus = 6;
            stock.PrevPriorPrice = 0.7;
            stock.PriorClosePrice = 0.8;
            stock.PriorPrice = 0.9;
            stock.SecurityName = "10";
            stock.StockType = "11";
            stock.TotalListingQtty = 12;
            stock.TradingUnit = 13;

            return stock;
        }

        [TestMethod]
        public void TestRemoveSpecialCharacters()        
        {
            StockInfoStatic stock = GetSpeicalStockInfoStatic();
            // add some special chars
            stock.SecurityName = "1|0";
            stock.StockType = "1#1";

            string expected = "0.1|0.2|3|0.4|05|6|0.7|0.8|0.9|1 0|1 1|12|13|";
            
            string actual = StockMessageBuilder.BuildMessage(stock);

            Assert.AreEqual(expected, actual, "Format message incorrect");
        }
    }
}
